package com.anysoft.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebView;

import com.anysoft.base.BaseWebViewFragment;
import com.anysoft.ui.activity.ParentHomeActivity;
import com.anysoft.ui.activity.web.ComMentorInfoActivity;
import com.anysoft.ui.activity.web.ParentZhiDaoInfoActivity;
import com.anysoft.utils.ActivityCollector;
import com.anysoft.utils.HttpUtil;
import com.anysoft.utils.NetWorkUtil;

import java.util.HashMap;

/**
 * Created by lty on 2018/7/2.
 */
public class ParentHomeFragment extends BaseWebViewFragment {
    
    private ParentHomeActivity activity;
    
    public static ParentHomeFragment getInstance() {
        return new ParentHomeFragment();
    }
    
    
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (ParentHomeActivity) context;
    }
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadUrl = generateUrl("parentHome", HttpUtil.getCommonParams2StringByWeb(activity));
        if (NetWorkUtil.isConnect(activity)) {
            setWebViewConfig(loadUrl);
        }
    }
    
    public void reloadPage() {
        setWebViewConfig(loadUrl);
    }
    
    @Override
    public void onResume() {
        super.onResume();
    }
    
    @Override
    public void onPause() {
        super.onPause();
    }
    
    @Override
    public void onshouldOverrideUrlLoadingCallBack(WebView webview, String url, String params) {
        HashMap<String, String> urlMap = handleUrlParams(url);
        String titleName = urlMap.get("title");
        if (url.contains("comMentorInfo.html")) {
            mIntent.putExtra("titleName", titleName);
            mIntent.putExtra("params", params);
            ActivityCollector.startTargetActivity(activity, ComMentorInfoActivity.class, mIntent);
        } else if (url.contains("parentZhiDaoInfo.html")) {
            mIntent.putExtra("titleName", titleName);
            mIntent.putExtra("params", params);
            ActivityCollector.startTargetActivity(activity, ParentZhiDaoInfoActivity.class, mIntent);
        } else {
            webview.loadUrl(url);
        }
    }
}
